﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TripsLogic.util;
using TripsLogic.model;
using System.Data;

namespace TripsLogic.controller
{
    public class SliderController
    {
        public static DataTable getSliders(int number, string groupId)
        {
            //get
            //test//
            try
            {
                DataTable dt = new DataTable();
                if (number > 0)
                {
                    object[,] parameters = { { "@groupId", groupId } };
                    dt = DB.getData("select TOP " + number + " * from SLIDER where groupId = @groupId order by PostedDate desc;", parameters);
                    return dt;
                }
                else
                    return null;

            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getSliders"));
                return null;
            }
        }

        public static DataTable getSliders(string groupId)
        {
            //get
            try
            {
                object[,] parameters = { { "@groupId", groupId } };
                return DB.getData("select Slider.*, [GROUP].Name " +
                    "from slider join [GROUP] on SLIDER.GroupId = [GROUP].Id " +
                    "where Slider.groupId = @groupId;", parameters);


            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getSliders"));
                return null;
            }
        }

        public static bool deleteSlider(string gvIDs)
        {
            try
            {
                return DB.exec("delete from Slider where id in " + gvIDs + ";");
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error deleteSlider"));
                return false;
            }
        }

        public static bool updateSlider(Slider slider)
        {
            try
            {
                object[,] parameters = {   {"@Id", slider.id},
                                           {"@Text", slider.text}, 
                                           {"@ActionUrl", slider.actionUrl},
                                           {"@PostedDate", slider.postedDate},
                                           {"@Flag", slider.flag},
                                           {"@GroupId", slider.groupId}
                                    };
                return DB.exec("update SLIDER set Text = @Text, ActionUrl = @ActionUrl, PostedDate = @PostedDate where Id = @Id;", parameters);
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error updateSlider"));
                return false;
            }
        }

        public static bool createSlider(Slider slider)
        {
            try
            {
                object[,] parameters = {   {"@Text", slider.text}, 
                                           {"@ActionUrl", slider.actionUrl},
                                           {"@PostedDate", slider.postedDate},
                                           {"@Flag", slider.flag},
                                           {"@GroupId", slider.groupId}
                                    };
                return DB.exec("insert into Slider (Text, ActionUrl, PostedDate, Flag, GroupId) values (@Text, @ActionUrl, @PostedDate, @Flag, @GroupId);", parameters);
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error createSlider"));
                return false;
            }
        }

        public static string getCurrentId()
        {
            try
            {
                return DB.getValue("select IDENT_CURRENT('SLIDER')");
            }
            catch (Exception ex)
            {
                // log
                //LogController.createLog(new Log(DateTime.Now.ToString("yyyyMMddHHmmss"), ex.Message, 1, "Error getCurrentSliderId"));
                return "";
            }
        }
    }
}